package com.toefl.speaking.activity.base;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.ihandy.core.ui.activity.ListActivity;
import com.toefl.speaking.helper.DialogHelper;
import com.toefl.speaking.util.MyAsyncTask;
import com.toefl.speaking.util.ProgressAsyncTask;

public abstract class StepListActivity<T> extends ListActivity<T> {

	/** 起始索引 */
	protected int mIndex = 0;
	/** 每页显示数据条数 */
	protected int mPageSize = 0;
	/** 是否启用更多加载 */
	private boolean mEnableMoreLoading = false;
	protected boolean mEnableAnim = true;
	public ProgressAsyncTask proTask;
	public Context context;
	protected MyAsyncTask userAsyncTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		onInit();
	}

	private OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			// case R.id.btn_pre:
			// reset();
			// onPre();
			// if (mEnableAnim) {
			// }
			// break;
			// case R.id.btn_next:
			// if (onNext()) {
			// if (mEnableAnim) {
			// Parent.animNext();
			// }
			// }
			// break;
			// case R.id.btn_middle:
			// onMiddle();
			// break;
			// case R.id.btn_more:
			// load();
			// break;
			}
		}
	};

	protected void userOnPost() {
	}

	public void userDoInBackground() {
	}

	/**
	 * 子类View可以通过这个方法获取一个asynctask 只需覆写userDoInBackground，userOnPost，然后执行这个task即可
	 * @return
	 */
	protected MyAsyncTask getUserAsyncTask() {
		userAsyncTask = new MyAsyncTask(context) {
			@Override
			protected void onPostExecute(Void result) {
				userOnPost();
				super.onPostExecute(result);
			}

			@Override
			protected Void doInBackground(Void... params) {
				userDoInBackground();
				return super.doInBackground(params);
			}
		};
		return userAsyncTask;
	}

	/**
	 * 初始化
	 */
	protected void onInit() {

	}

	@Override
	protected ArrayList<T> loadData() {
		return loadData(mIndex, mPageSize);
	}

	private View footer;

	@Override
	protected void onComplate(ArrayList<T> mTempValues, String errorMsg) {
		if (mEnableMoreLoading) {
			if (!TextUtils.isEmpty(errorMsg)) {
				DialogHelper.showToastCanceledOnTouchOutside(StepListActivity.this, null, errorMsg);
			} else {
				if (mListView instanceof ListView) {
					ListView list = (ListView) mListView;
					if (mTempValues != null) {
						int size = mTempValues.size();
						// 首次加载
						if (mIndex == 0) {
							// 一页显示数据量
							if (size >= mPageSize && list.getFooterViewsCount() == 0) {
								// footer = mInflater.inflate(R.layout.list_more_item, null);
								footer.setOnClickListener(onClick);
								// footer.findViewById(R.id.btn_more).setOnClickListener(onClick);
								list.addFooterView(footer);
							} else if (size < mPageSize && footer != null) {
								list.removeFooterView(footer);
							}
							mIndex = size;
							mObjects = mTempValues;
							mListView.setAdapter(this);
						} else {
							if (size < mPageSize && footer != null) {
								list.removeFooterView(footer);
							}
							if (size == 0) {
								DialogHelper
										.showToastCanceledOnTouchOutside(StepListActivity.this, null, "没有更多数据可以加载!");
								return;
							}

							synchronized (mObjects) {
								if (mIndex == 0) {
									mObjects.clear();
								}
								// 分页显示
								mIndex += size;
								mObjects.addAll(mTempValues);
							}
							notifyDataSetChanged();
						}
					}
				}
			}
		} else {
			super.onComplate(mTempValues, errorMsg);
		}
	}

	/**
	 * 启用更多
	 * 
	 * @param index 默认从0开始索引
	 * @param pagesize 每页显示数量
	 */
	public void enableMoreLoading(int index, int pagesize) {
		mEnableMoreLoading = true;
		this.mIndex = index;
		this.mPageSize = pagesize;
	}

	/**
	 * 分页加载
	 * 
	 * @param index
	 * @param pageSize
	 * @return
	 */
	protected ArrayList<T> loadData(int index, int pageSize) {
		return new ArrayList<T>();
	}

	/***
	 * 以对话框形式 弹出提示 信息
	 * @param msg
	 */
	public void showMsgInDialog(String msg) {
		DialogHelper.showToastCanceledOnTouchOutside(this, null, msg);
	}

}
